<?php

/**
 * Created by PhpStorm.
 * User: WhiteBlue
 * Date: 15/3/2
 * Time: 下午3:49
 */
class dispatcher
{
    public static function dispatch($router, $config)
    {

        if ($config->getConfig('isDebug') == true) {
            error_reporting(E_ALL);
            echo '路径:' . $router->getAction() . '||';
            echo '控制器:' . $router->getController() . '||';
            echo '方法:' . $router->getMethod() . '||';
            echo '参数:';
            var_dump($router->getParams());
            echo '||';
        } else {
            error_reporting(0);
        }
        global $app;
        $controller = $router->getController();
        $action = $router->getAction();
        $controller_file = "app/controllers/{$controller}.php";
        if (file_exists($controller_file)) {
            require_once($controller_file);
            $app = new $controller();
            $app->setMethod($router->getMethod());
            $app->setParams($router->getParams());
            $app->$action();

        } else {
            throw new Exception("404 Not Found");
        }
    }
}